//
//  MYStatusViewModel.swift
//  MYWeiBo
//
//  Created by apple on 16/5/15.
//  Copyright © 2016年 YanMo. All rights reserved.
//

import UIKit

class MYStatusViewModel: NSObject {
    //创建单例有两个步骤 单例  私有init的方法
    static let sharedInstance:MYStatusViewModel = MYStatusViewModel()
    private override init() {
        super.init()
    }
    
    //加载微博数据
    func loadStatus(loadStatusCallBack:(statuses:[MYStatus]?,error:NSError?) -> ()){
        //当没有access_token时不登录
        guard let access_token = MYUserAccountModel.sharedInstance.userAccount?.access_token else{
            MYPrint(items: "access_token没有数据")
            return
        }
        //打通数据
            //发送网络请求
            let URLString = "/2/statuses/home_timeline.json"
            let parameters = [
                
                "access_token" : access_token
            ]
            MYAFNetworkingTool.shardInstance.request(requestmeMethod.GET, URLString: URLString, parameters: parameters, success: { (_, responseObject) -> Void in
//                MYPrint(items: "成功的取得数据：responseObject\(responseObject)")
                    if let result = responseObject as? [String:AnyObject]{
                        if let statuses = result["statuses"] as? [[String:AnyObject]]{
                            //遍历数组
                            //定义一个空数组
                            var statusArr = [MYStatus]()
                            for dict in statuses{
                                let status = MYStatus(dict: dict)
                                statusArr.append(status)
                            }
//                            MYPrint(items: "转换后的微博数组statusArr:\(statusArr)")
                            loadStatusCallBack(statuses: statusArr, error: nil)
                        }
                        
                    }
                
                }, failure: { (_, error) -> Void in
                    MYPrint(items: "失败，不能取得数据")
                    loadStatusCallBack(statuses: nil, error: error)
            })
        
    }

}
